package com.platform.service.impl;

import com.platform.dao.GoodsDao;
import com.platform.dao.GoodsSpecificationDao;
import com.platform.entity.GoodsEntity;
import com.platform.entity.GoodsSpecificationEntity;
import com.platform.utils.BeanUtils;
import com.platform.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.platform.dao.ProductDao;
import com.platform.entity.ProductEntity;
import com.platform.service.ProductService;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service实现类
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-30 14:31:21
 */
@Service("productService")
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private GoodsSpecificationDao goodsSpecificationDao;

    @Override
    public ProductEntity queryObject(Integer id) {
        return productDao.queryObject(id);
    }

    @Override
    public ProductEntity getInfoDetail(Integer id) {
        ProductEntity item = productDao.queryObject(id);
        GoodsEntity goods = goodsDao.queryObject(item.getGoodsId());
        if(goods != null) {
            item.setGoodsName(goods.getName());
        }
        convertSpecificationName(item);
        return item;
    }

    @Override
    public List<ProductEntity> queryList(Map<String, Object> map) {
        List<ProductEntity> list = productDao.queryList(map);

        List<ProductEntity> result = new ArrayList<>();
        //翻译产品规格
        if (null != list && list.size() > 0) {
            for (ProductEntity item : list) {
                convertSpecificationName(item);
                result.add(item);
            }
        }
        return result;
    }

    /**
     * 将规格编码转换为中文名称
     * @param item
     */
    private void convertSpecificationName(ProductEntity item){
        String specificationIds = item.getGoodsSpecificationIds();
        String specificationsName = "";
        if (!StringUtils.isNullOrEmpty(specificationIds)) {
            String[] arr = specificationIds.split("_");

            for (String goodsSpecificationId : arr) {
                GoodsSpecificationEntity entity = goodsSpecificationDao.queryObject(goodsSpecificationId);
                if (null != entity) {
                    specificationsName += entity.getValue() + "；";
                }
            }
        }
        item.setSpecificationsName(specificationsName);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return productDao.queryTotal(map);
    }

    @Override
    @Transactional
    public int save(ProductEntity product) {
        int result = 0;
        if (product != null && product.getGoodsId() != null && product.getGoodsSpecificationIds() != null) {
            int exist = productDao.querySameGoodsSpecification(product.getGoodsId(), product.getGoodsSpecificationIds());
            if(exist <= 0) {
                result += productDao.save(product);
                result = 1;
            }
        }
        return result;
    }

    @Override
    public int update(ProductEntity product) {
        if (StringUtils.isNullOrEmpty(product.getGoodsSpecificationIds())){
            product.setGoodsSpecificationIds("");
        }
        return productDao.update(product);
    }

    @Override
    public int delete(Integer id) {
        return productDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return productDao.deleteBatch(ids);
    }
}
